package com.muatocroi.tornado;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ActivityMCAbstractClass implements OnClickListener{
	
	EditText edPass;
	SharedPreferences settings;
	static SignalSender sender = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        Button btnConnect = (Button)findViewById(R.id.btnConnect);
        edPass = (EditText)findViewById(R.id.edPass);
        
        btnConnect.setOnClickListener(this);
        
        settings = getSharedPreferences("AppConfig", MODE_PRIVATE);
        int password = settings.getInt("PASSWORD", 0);
        edPass.setText(String.valueOf(password));
        
        sender = SignalSender.getSender();
        ArrayList<InetAddress> broadcasts = AddressLocal.getBroadcasts();
        if (broadcasts.size() == 0) {
        	Toast.makeText(this,"Your have no network connection, please read help for more.", Toast.LENGTH_SHORT).show();
        	return;
		}
        else
        {
        	CommandSignal.setBroadcastAddress(broadcasts.get(0));
        }
        if (!sender.isSending())
        {
			sender.BeginSend();
        }
        
    }
	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btnConnect:
			int pass = 0;
			try {
				pass = Integer.parseInt(edPass.getText().toString());
			} catch (Exception e) {
				
			}
			CommandSignal.password = (short)pass;
			try {
				//write password to preferences file
				SharedPreferences.Editor editor = settings.edit();
				editor.putInt("PASSWORD",pass);
				editor.commit();
				
				//Debug = true
		        ArrayList<InetAddress> broadcasts = AddressLocal.getBroadcasts();
		        if (broadcasts.size() == 0) {
		        	Toast.makeText(this,"Your have no network connection, please read help for more.", Toast.LENGTH_SHORT).show();
		        	return;
				}
		        else
		        {
		        	CommandSignal.setBroadcastAddress(broadcasts.get(0));
		        }
		        if (!sender.isSending())
		        {
					sender.BeginSend();
		        }
				//*/
				
				Intent intent = new Intent(this, ControlerActivity.class);
		        startActivity(intent);
			} catch (Exception e) {
				break;
			}
			
	        ArrayList<InetAddress> broadcasts = AddressLocal.getBroadcasts();
	        if (broadcasts.size() == 0) {
	        	Toast.makeText(getApplicationContext(),"Your have no network connection, please read help for more.", Toast.LENGTH_LONG).show();
				return;
			}
	        else
	        {
	        	CommandSignal.setBroadcastAddress(broadcasts.get(0));
	        }
			break;
		default:
			break;
		}
	}
}
